<template>
  <div class="table_content">
    <el-card>
      <div style="text-align: right; margin-bottom: 20px">
        <el-button type="primary" size="small" @click="addMenu">添加菜单</el-button>
      </div>
      <div class="table">
        <el-table
          :data="tableData"
          size="small"
          :height="tbHeight"
          :header-cell-style="{background:'#FAFAFA'}"
          :row-style="{height:'58px'}"
        >
          <el-table-column prop="name" label="菜单名称" align="center" />
          <el-table-column prop="uniqueName" label="菜单标识" align="center" />
          <el-table-column prop="type" label="菜单类型" align="center">
            <template slot-scope="scope">
              <div>
                {{ scope.row.type === '1' ? '菜单' : '权限' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <span class="msg99" @click="editCarbon(scope.row)">编辑</span>
              <span class="msg88" @click="delCarbon(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!-- <div class="pagination">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
            :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next" :total="total">
          </el-pagination>
        </div> -->
      </div>
    </el-card>
    <!-- 弹框 -->
    <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
      <el-form ref="formName" v-loading="loading" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单标识" prop="uniqueName">
          <el-input v-model="formData.uniqueName" placeholder="请输入菜单标识" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 由数字、26个英文字母或者下划线组成的字符串
const validatePass = (rule, value, callback) => {
  if (!/^[0-9a-zA-Z_]{1,}$/.test(value)) {
    callback(new Error('菜单标识只能包含中英文数字下划线'))
  } else {
    callback()
  }
}
export default {
  name: 'Menu1',
  data() {
    return {
      tableData: [],
      tbHeight: 540,
      showTitle: '新增',
      showDialog: false,
      loading: false,
      formData: {
        name: '',
        type: '1',
        uniqueName: ''
      },
      pageIndex: 1, // 当前多少页
      pageSizes: [10, 30, 50, 100], // 显示多少条
      total: 0, // 总条数
      pageSize: 10, // 每页多少条
      Update: false,
      rules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: ['blur'] },
          { max: 100, message: '菜单名称长度最大100', trigger: ['blur'] }
        ],
        uniqueName: [
          { required: true, message: '请输入菜单标识', trigger: ['blur'] },
          { max: 100, message: '菜单标识长度最大100', trigger: ['blur'] },
          { validator: validatePass, trigger: ['blur'] }
        ]
      }

    }
  },
  created() {
    const that = this
    // let ctHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
    // that.tbHeight = ctHeight - 310
    that.rbacMenuQueryAll()
  },
  methods: {
    // 添加菜单按钮
    addMenu() {
      const that = this
      that.showDialog = true
      that.showTitle = '新增菜单'
    },
    // 删除按钮
    delCarbon(row) {
      const that = this
      that.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.rbacMenuDelete(row.id)
      }).catch(() => { })
    },
    // 编辑按钮
    editCarbon(row) {
      const that = this
      // console.log(row, '编辑按钮')
      that.showTitle = '编辑菜单'
      that.showDialog = true
      that.Update = true
      that.formData = {
        ...row
      }
    },
    // 删除菜单 接口
    rbacMenuDelete(id) {
      const that = this
      const data = {
        id
      }
      that.$store.dispatch('menu/rbacMenuDelete', data).then(res => {
        // console.log(res, '删除')
        if (res.data) {
          that.rbacMenuQueryAll()
          that.$message({
            type: 'success',
            message: '删除成功'
          })
        }
      })
    },
    // 更新菜单(编辑)
    rbacMenuUpdate() {
      const that = this
      const data = that.formData
      that.$store.dispatch('menu/rbacMenuUpdate', data).then(res => {
        // console.log(res, '编辑')
        if (res.data) {
          that.showDialog = false
          that.formData = {
            name: '',
            type: '1',
            uniqueName: ''
          }
          that.$message.success('编辑成功')
          that.rbacMenuQueryAll()
        }
      })
    },
    // 查询全部菜单
    rbacMenuQueryAll() {
      const that = this
      that.$store.dispatch('menu/rbacMenuQueryAll').then(res => {
        // console.log(res, '查询全部菜单')
        if (res.data) {
          that.tableData = res.data
          that.total = res.data.length
        }
      })
    },
    // 新增接口
    rbacMenuAdd() {
      const that = this
      const data = that.formData
      that.$store.dispatch('menu/rbacMenuAdd', data).then(res => {
        // console.log(res, '新增菜单')
        if (res.data) {
          that.showDialog = false
          that.formData = {
            name: '',
            type: '1',
            uniqueName: ''
          }
          that.$message.success('添加成功')
          that.rbacMenuQueryAll()
        }
      })
    },
    // 弹框确认
    clickSubmit() {
      const that = this
      this.$refs.formName.validate((valid) => {
        if (valid) {
          if (that.Update) {
            that.rbacMenuUpdate()
          } else {
            that.rbacMenuAdd()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 弹框取消
    closeDialog() {
      const that = this
      that.showDialog = false
      that.Update = false
      that.formData = {
        name: '',
        type: '1',
        uniqueName: ''
      }
    },
    // 每页多少条
    handleSizeChange(val) {
      this.pageSize = val
      this.searchReport()
    },
    // 当前页
    handleCurrentChange(val) {
      this.pageIndex = val
      this.searchReport()
    }

  }

}
</script>

<style lang="scss" scoped>
::v-deep .el-card__body {
  padding: 20px;
}
::v-deep .el-dialog__header_xqh {
  padding: 20px 20px 10px;
  background: #66b1ff;
  .el-dialog__title {
    color: #fff;
  }
}
.table_content {
  margin-top: 20px;
  width: 100%;
  background-color: #fff;
  border-radius: 4px;
  padding: 0 20px 20px 20px;
}
.table {
  overflow: auto;
  height: 1600%;
  box-sizing: border-box;
  .empty {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    .img_w_h {
      width: 56px;
      height: 56px;
    }
  }
  .pagination {
    display: flex;
    justify-content: right;
    margin-top: 30px;
  }
}
</style>
